package com.imessage.action.business.apple;

import com.bean.BusinessApple;
import com.imessage.comm.Constants;
import com.imessage.type.BusinessAppleState;
import com.imessage.vo.AppleVo;
import com.imessage.web.Page;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/apple")
public class AppleAction extends AppleBaseAction {

    /**
     * 列表数据,搜索数据
     */
    @RequestMapping(value = "")
    public Object run(AppleVo appleVo) {
        List<Expression> expressionList = new ArrayList<>();
        setExpressionList(expressionList, appleVo);
        Page<BusinessApple> page = baseService.findPage(BusinessApple.class, expressionList,
                null, getOrder(appleVo.getOrders()), appleVo.getPageCurrent(),
                appleVo.getPageSize());
        return page;
    }


    /**
     * 添加修改界面
     */
    @RequestMapping(value = "/initpage")
    public ModelAndView initpage(
            @RequestParam(name = "req_id", required = true) String id) {
        ModelAndView modelAndView = new ModelAndView();
        BusinessApple apple = new BusinessApple();
        if (StringUtils.isNotBlank(id)) {
            apple = baseService.findById(BusinessApple.class, id);
            modelAndView.setViewName("admin/business/apple/modify");
        } else {
            modelAndView.setViewName("admin/business/apple/add");
        }
        setModelAndView(modelAndView, apple);
        return modelAndView;
    }


    /**
     * 添加
     */
    @RequestMapping(value = "/add")
    public Object add(AppleVo appleVo) {
        String msg=checkData(appleVo,null);
        if(StringUtils.isNotBlank(msg)){
            return getJsonForErr(msg, datagrid);
        }
        BusinessApple apple = new BusinessApple();
        setApple(appleVo, apple);
        int state = baseService.saveObject(apple);
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }




    /**
     * 修改
     */
    @RequestMapping(value = "/modify")
    public Object modify(AppleVo appleVo) {
        BusinessApple apple = baseService.findById(BusinessApple.class, appleVo.getId());
        String msg=checkData(appleVo,apple);
        if(StringUtils.isNotBlank(msg)){
            return getJsonForErr(msg, datagrid);
        }
        setApple(appleVo, apple);
        int state = baseService.updateObject(apple);
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return apple;
        }
        return getJsonForErr("", datagrid);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/del")
    public Object del(
            @RequestParam(name = "id", required = true) String ids) {
        int state = Constants.STATE_OPERATOR_LOST;
        for (String id : ids.split(",")) {
            BusinessApple apple = baseService.findById(BusinessApple.class, id);
            state = baseService.delObject(apple);
        }
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }



    @RequestMapping(value = "/initimp")
    public ModelAndView initimp() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("action", "/admin/apple/imp");
        modelAndView.addObject("templeturl","/static/file/appleid.xlsx");
        modelAndView.setViewName("admin/comm/imp");
        return modelAndView;
    }

    @RequestMapping(value = "/imp")
    public Object imp(MultipartFile upfile){
        String filePath=fileUpload(upfile);
            if(StringUtils.isBlank(filePath)){
            return getJsonForErr("上传文件为空或者格式不正确,只支持xls、xlsx格式", datagrid);
        }
        if(saveExcel(filePath)){
            return getJsonForSucc("上传成功", datagrid);
        }
        return getJsonForErr("上传失败", datagrid);
    }

    /**
     * 批量状态
     */
    @RequestMapping(value = "/state")
    public Object state(
            @RequestParam(name = "id", required = true) String ids,
            @RequestParam(name = "state", required = true) String state) {
        int result = Constants.STATE_OPERATOR_LOST;
        for (String id : ids.split(",")) {
            BusinessApple apple = baseService.findById(BusinessApple.class, id);
            apple.setState(BusinessAppleState.fromValue(state));
            result = baseService.updateObject(apple);
        }
        if (result == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }
}
